<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>事件</title>
    <script src="./vue.js"></script>
  </head>
  <body>
    <div id="example-1">
      <button v-on:click="counter += 1">Add 1</button>
      <p>The button above has been clicked {{ counter }} times.</p>
    </div>
    <div id="example-2">
      <!-- `greet` 是在下面定义的方法名 -->
      <button v-on:click="greet">Greet</button>
    </div>
    <!-- 内联中直接调用 -->
    <div id="example-3">
      <button v-on:click="say('hi')">Say hi</button>
      <button v-on:click="say('what')">Say what</button>
      <!-- 内联中访问原始事件 -->
      <button v-on:click="warn('Form cannot be submitted yet.', $event)">
        Submit
      </button>
    </div>
    <script>
      var example1 = new Vue({
        el: "#example-1",
        data: {
          counter: 0,
        },
      });
      var example2 = new Vue({
        el: "#example-2",
        data: {
          name: "Vue.js",
        },
        // 在 `methods` 对象中定义方法
        methods: {
          greet: function (event) {
            // `this` 在方法里指向当前 Vue 实例
            alert("Hello " + this.name + "!");
            // `event` 是原生 DOM 事件
            if (event) {
              alert(event.target.tagName);
            }
          },
        },
      });
      var example3 = new Vue({
        el: "#example-3",
        methods: {
          say: function (message) {
            alert(message);
          },
          warn: function (message, event) {
            // 现在我们可以访问原生事件对象
            if (event) {
              event.preventDefault();
            }
            alert(message);
          },
        },
      });
    </script>
  </body>
</html>
